package com.ljy.tkuser.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljy.tkuser.client.TkClient;
import com.ljy.tkuser.entity.LjyBigUser;
import com.ljy.tkuser.entity.reqvo.ReqVOLjyBigUser;
import com.ljy.tkuser.service.LjyBigUserService;
import com.ljy.tkuser.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * <p>
 * 用户登录表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-27
 */
@RestController
@RequestMapping("/ljy-big-user")
public class LjyBigUserController {
    @Autowired
    LjyBigUserService ljyBigUserService;

    @Autowired
    TkClient tkClient;
    //分页条件查询
    @PostMapping("UserPage")
    public HashMap<String, Object> pageUserConditionQuery(@RequestBody(required = false) ReqVOLjyBigUser reqVOLjyBigUser) {
        HashMap<String, Object> map = ljyBigUserService.pageUserConditionSelect(reqVOLjyBigUser);
        return map;
    }

    @PostMapping("saveUserInfo")
    public String saveUserInfo(@RequestBody ReqVOLjyBigUser reqVOLjyBigUser) {
        int info = ljyBigUserService.insertUserInfo(reqVOLjyBigUser);
        if (info > 0) {
            return "save success";
        } else {
            return "error";
        }
    }

    @PutMapping("updateUserInfo")
    public String updateUserInfo(@RequestBody LjyBigUser ljyBigUser) {
        int num = ljyBigUserService.PutUserInfo(ljyBigUser);
        if (num > 0) {
            return "update user success";
        } else {
            return "update user error";
        }
    }

    @DeleteMapping("deleteUserInfo")
    public String deleteUserInfo(@RequestParam Integer userId) {
        boolean id = ljyBigUserService.removeById(userId);
        if (id) {
            return "成功";
        } else {
            return "失败";
        }
    }

    //登录
    @PostMapping("loginUser")
    public HashMap<String, Object> loginUser(@RequestParam(required = false) String nikeName, @RequestParam String passWord,HttpServletResponse response) throws UnsupportedEncodingException {
        return ljyBigUserService.UserConditionSelect(nikeName, passWord,response);
    }

    //推出登录
    @PostMapping("logoutUser")
    public void logoutUser(HttpSession session) {
        ljyBigUserService.loutUser(session);
    }

    //获取用户信息
    @GetMapping("getUserInfo")
    public LjyBigUser getUserInfo(HttpServletRequest  request) throws UnsupportedEncodingException {
//         String userID = JwtUtils.getMemberIdByJwtToken(request);
//         LjyBigUser user = ljyBigUserService.getById(userID);
        return ljyBigUserService.getUser(request);

    }

    //测试feign
    @GetMapping("TestFeign")
    public void TestFeign(){
        tkClient.queryConditionTeacher(new HashMap());
    }
}

